<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>

	
		<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
		<meta http-equiv="Content-Language" content="en-us">

		<title>Django | URL dispatcher | Django Documentation</title>

		<meta name="ROBOTS" content="ALL">
		<meta http-equiv="imagetoolbar" content="no">
		<meta name="MSSmartTagsPreventParsing" content="true">
		<meta name="Copyright" content="This site's design and contents Copyright (c) 2005  Lawrence Journal-World.">

		<meta name="keywords" content="Python, Django, framework, open-source">
		<meta name="description" content="Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.">

		<link href="Django%20%7C%20URL%20dispatcher%20%7C%20Django%20Documentation_files/base.css" rel="stylesheet" type="text/css" media="screen">
		<link href="Django%20%7C%20URL%20dispatcher%20%7C%20Django%20Documentation_files/print.css" rel="stylesheet" type="text/css" media="print">
      
  
  <style type="text/css" media="screen">
    #docs-search {
      color: #000;
      float: right;
    }
    #docs-search form {
      font-size: 92%;
      margin: 0;
      padding: 1em 1em 0;
      white-space: nowrap;
    }
    form.search ul {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    form.search li {
      display: inline;
      padding-right: 1em;
    }
    form.search .query {
      width: 18em;
    }
  </style>
  <link rel="stylesheet" href="Django%20%7C%20URL%20dispatcher%20%7C%20Django%20Documentation_files/pygments.css" type="text/css">

	</head><body id="documentation" class="default">

	<div id="container">
		<div id="header">
			<h1 id="logo"><a href="http://www.djangoproject.com/"><img src="Django%20%7C%20URL%20dispatcher%20%7C%20Django%20Documentation_files/hdr_logo.gif" alt="Django"></a></h1>
			<ul id="nav-global">
				<li id="nav-homepage"><a href="http://www.djangoproject.com/">Home</a></li>
				<li id="nav-download"><a href="http://www.djangoproject.com/download/">Download</a></li>
				<li id="nav-documentation"><a href="http://docs.djangoproject.com/">Documentation</a></li>
				<li id="nav-weblog"><a href="http://www.djangoproject.com/weblog/">Weblog</a></li>
				<li id="nav-community"><a href="http://www.djangoproject.com/community/">Community</a></li>
				<li id="nav-code"><a href="http://code.djangoproject.com/">Code</a></li>
			</ul>
		</div>
		<!-- END Header -->
		<div id="billboard">
  <h2><a href="http://docs.djangoproject.com/en/1.0/">Django documentation</a></h2>
</div>
		<div id="columnwrap">
			
		<div id="content-main">
		


  <h2 class="deck">
  
    This document describes Django version 1.0. For development documentation, 
    <a href="http://docs.djangoproject.com/en/dev/topics/http/urls/">go here</a>.
  
  </h2>
  <div class="section" id="s-url-dispatcher">
<span id="s-topics-http-urls"></span><span id="url-dispatcher"></span><span id="topics-http-urls"></span><h1>URL dispatcher<a class="headerlink" href="#url-dispatcher" title="Permalink to this headline">¶</a></h1>
<p>A clean, elegant URL scheme is an important detail in a high-quality Web
application. Django lets you design URLs however you want, with no framework
limitations.</p>
<p>There’s no <tt class="docutils literal"><span class="pre">.php</span></tt> or <tt class="docutils literal"><span class="pre">.cgi</span></tt> required, and certainly none of that
<tt class="docutils literal"><span class="pre">0,2097,1-1-1928,00</span></tt> nonsense.</p>
<p>See <a class="reference external" href="http://www.w3.org/Provider/Style/URI">Cool URIs don’t change</a>, by World Wide Web creator Tim Berners-Lee, for
excellent arguments on why URLs should be clean and usable.</p>
<div class="section" id="s-overview">
<span id="overview"></span><h2>Overview<a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h2>
<p>To design URLs for an app, you create a Python module informally called a
<strong>URLconf</strong> (URL configuration). This module is pure Python code and
is a simple mapping between URL patterns (as simple regular expressions) to
Python callback functions (your views).</p>
<p>This mapping can be as short or as long as needed. It can reference other
mappings. And, because it’s pure Python code, it can be constructed
dynamically.</p>
</div>
<div class="section" id="s-id1">
<span id="s-how-django-processes-a-request"></span><span id="id1"></span><span id="how-django-processes-a-request"></span><h2>How Django processes a request<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h2>
<p>When a user requests a page from your Django-powered site, this is the
algorithm the system follows to determine which Python code to execute:</p>
<ol class="arabic simple">
<li>Django determines the root URLconf module to use. Ordinarily,
this is the value of the <tt class="docutils literal"><span class="pre">ROOT_URLCONF</span></tt> setting, but if the incoming
<tt class="docutils literal"><span class="pre">HttpRequest</span></tt> object has an attribute called <tt class="docutils literal"><span class="pre">urlconf</span></tt>, its value
will be used in place of the <tt class="docutils literal"><span class="pre">ROOT_URLCONF</span></tt> setting.</li>
<li>Django loads that Python module and looks for the variable
<tt class="docutils literal"><span class="pre">urlpatterns</span></tt>. This should be a Python list, in the format returned by
the function <tt class="docutils literal"><span class="pre">django.conf.urls.defaults.patterns()</span></tt>.</li>
<li>Django runs through each URL pattern, in order, and stops at the first
one that matches the requested URL.</li>
<li>Once one of the regexes matches, Django imports and calls the given
view, which is a simple Python function. The view gets passed an
<a title="django.http.HttpRequest" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/request-response/#django.http.HttpRequest"><tt class="xref docutils literal"><span class="pre">HttpRequest</span></tt></a> as its first argument and any values
captured in the regex as remaining arguments.</li>
</ol>
</div>
<div class="section" id="s-example">
<span id="example"></span><h2>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
<p>Here’s a sample URLconf:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.conf.urls.defaults</span> <span class="k">import</span> <span class="o">*</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^articles/2003/$'</span><span class="p">,</span> <span class="s">'news.views.special_case_2003'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^articles/(\d{4})/$'</span><span class="p">,</span> <span class="s">'news.views.year_archive'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^articles/(\d{4})/(\d{2})/$'</span><span class="p">,</span> <span class="s">'news.views.month_archive'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^articles/(\d{4})/(\d{2})/(\d+)/$'</span><span class="p">,</span> <span class="s">'news.views.article_detail'</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Notes:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">from</span> <span class="pre">django.conf.urls.defaults</span> <span class="pre">import</span> <span class="pre">*</span></tt> makes the <tt class="docutils literal"><span class="pre">patterns()</span></tt>
function available.</li>
<li>To capture a value from the URL, just put parenthesis around it.</li>
<li>There's no need to add a leading slash, because every URL has that. For
example, it's <tt class="docutils literal"><span class="pre">^articles</span></tt>, not <tt class="docutils literal"><span class="pre">^/articles</span></tt>.</li>
<li>The <tt class="docutils literal"><span class="pre">'r'</span></tt> in front of each regular expression string is optional but
recommended. It tells Python that a string is "raw" -- that nothing in
the string should be escaped. See <a class="reference external" href="http://diveintopython.org/regular_expressions/street_addresses.html#re.matching.2.3">Dive Into Python's explanation</a>.</li>
</ul>
<p>Example requests:</p>
<ul class="simple">
<li>A request to <tt class="docutils literal"><span class="pre">/articles/2005/03/</span></tt> would match the third entry in the
list. Django would call the function
<tt class="docutils literal"><span class="pre">news.views.month_archive(request,</span> <span class="pre">'2005',</span> <span class="pre">'03')</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">/articles/2005/3/</span></tt> would not match any URL patterns, because the
third entry in the list requires two digits for the month.</li>
<li><tt class="docutils literal"><span class="pre">/articles/2003/</span></tt> would match the first pattern in the list, not the
second one, because the patterns are tested in order, and the first one
is the first test to pass. Feel free to exploit the ordering to insert
special cases like this.</li>
<li><tt class="docutils literal"><span class="pre">/articles/2003</span></tt> would not match any of these patterns, because each
pattern requires that the URL end with a slash.</li>
<li><tt class="docutils literal"><span class="pre">/articles/2003/03/3/</span></tt> would match the final pattern. Django would call
the function <tt class="docutils literal"><span class="pre">news.views.article_detail(request,</span> <span class="pre">'2003',</span> <span class="pre">'03',</span> <span class="pre">'3')</span></tt>.</li>
</ul>
</div>
<div class="section" id="s-named-groups">
<span id="named-groups"></span><h2>Named groups<a class="headerlink" href="#named-groups" title="Permalink to this headline">¶</a></h2>
<p>The above example used simple, <em>non-named</em> regular-expression groups (via
parenthesis) to capture bits of the URL and pass them as <em>positional</em> arguments
to a view. In more advanced usage, it's possible to use <em>named</em>
regular-expression groups to capture URL bits and pass them as <em>keyword</em>
arguments to a view.</p>
<p>In Python regular expressions, the syntax for named regular-expression groups
is <tt class="docutils literal"><span class="pre">(?P&lt;name&gt;pattern)</span></tt>, where <tt class="docutils literal"><span class="pre">name</span></tt> is the name of the group and
<tt class="docutils literal"><span class="pre">pattern</span></tt> is some pattern to match.</p>
<p>Here's the above example URLconf, rewritten to use named groups:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^articles/2003/$'</span><span class="p">,</span> <span class="s">'news.views.special_case_2003'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^articles/(?P&lt;year&gt;\d{4})/$'</span><span class="p">,</span> <span class="s">'news.views.year_archive'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^articles/(?P&lt;year&gt;\d{4})/(?P&lt;month&gt;\d{2})/$'</span><span class="p">,</span> <span class="s">'news.views.month_archive'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^articles/(?P&lt;year&gt;\d{4})/(?P&lt;month&gt;\d{2})/(?P&lt;day&gt;\d+)/$'</span><span class="p">,</span> <span class="s">'news.views.article_detail'</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>This accomplishes exactly the same thing as the previous example, with one
subtle difference: The captured values are passed to view functions as keyword
arguments rather than positional arguments. For example:</p>
<ul class="simple">
<li>A request to <tt class="docutils literal"><span class="pre">/articles/2005/03/</span></tt> would call the function
<tt class="docutils literal"><span class="pre">news.views.month_archive(request,</span> <span class="pre">year='2005',</span> <span class="pre">month='03')</span></tt>, instead
of <tt class="docutils literal"><span class="pre">news.views.month_archive(request,</span> <span class="pre">'2005',</span> <span class="pre">'03')</span></tt>.</li>
<li>A request to <tt class="docutils literal"><span class="pre">/articles/2003/03/3/</span></tt> would call the function
<tt class="docutils literal"><span class="pre">news.views.article_detail(request,</span> <span class="pre">year='2003',</span> <span class="pre">month='03',</span> <span class="pre">day='3')</span></tt>.</li>
</ul>
<p>In practice, this means your URLconfs are slightly more explicit and less prone
to argument-order bugs -- and you can reorder the arguments in your views'
function definitions. Of course, these benefits come at the cost of brevity;
some developers find the named-group syntax ugly and too verbose.</p>
<div class="section" id="s-the-matching-grouping-algorithm">
<span id="the-matching-grouping-algorithm"></span><h3>The matching/grouping algorithm<a class="headerlink" href="#the-matching-grouping-algorithm" title="Permalink to this headline">¶</a></h3>
<p>Here's the algorithm the URLconf parser follows, with respect to named groups
vs. non-named groups in a regular expression:</p>
<p>If there are any named arguments, it will use those, ignoring non-named arguments.
Otherwise, it will pass all non-named arguments as positional arguments.</p>
<p>In both cases, it will pass any extra keyword arguments as keyword arguments.
See "Passing extra options to view functions" below.</p>
</div>
</div>
<div class="section" id="s-what-the-urlconf-searches-against">
<span id="what-the-urlconf-searches-against"></span><h2>What the URLconf searches against<a class="headerlink" href="#what-the-urlconf-searches-against" title="Permalink to this headline">¶</a></h2>
<p>The URLconf searches against the requested URL, as a normal Python string. This
does not include GET or POST parameters, or the domain name.</p>
<p>For example, in a request to <tt class="docutils literal"><span class="pre">http://www.example.com/myapp/</span></tt>, the URLconf
will look for <tt class="docutils literal"><span class="pre">myapp/</span></tt>.</p>
<p>In a request to <tt class="docutils literal"><span class="pre">http://www.example.com/myapp/?page=3</span></tt>, the URLconf will look
for <tt class="docutils literal"><span class="pre">myapp/</span></tt>.</p>
<p>The URLconf doesn't look at the request method. In other words, all request
methods -- <tt class="docutils literal"><span class="pre">POST</span></tt>, <tt class="docutils literal"><span class="pre">GET</span></tt>, <tt class="docutils literal"><span class="pre">HEAD</span></tt>, etc. -- will be routed to the same
function for the same URL.</p>
</div>
<div class="section" id="s-syntax-of-the-urlpatterns-variable">
<span id="syntax-of-the-urlpatterns-variable"></span><h2>Syntax of the urlpatterns variable<a class="headerlink" href="#syntax-of-the-urlpatterns-variable" title="Permalink to this headline">¶</a></h2>
<p><tt class="docutils literal"><span class="pre">urlpatterns</span></tt> should be a Python list, in the format returned by the function
<tt class="docutils literal"><span class="pre">django.conf.urls.defaults.patterns()</span></tt>. Always use <tt class="docutils literal"><span class="pre">patterns()</span></tt> to create
the <tt class="docutils literal"><span class="pre">urlpatterns</span></tt> variable.</p>
<p>Convention is to use <tt class="docutils literal"><span class="pre">from</span> <span class="pre">django.conf.urls.defaults</span> <span class="pre">import</span> <span class="pre">*</span></tt> at the top of
your URLconf. This gives your module access to these objects:</p>
<div class="section" id="s-patterns">
<span id="patterns"></span><h3>patterns<a class="headerlink" href="#patterns" title="Permalink to this headline">¶</a></h3>
<p>A function that takes a prefix, and an arbitrary number of URL patterns, and
returns a list of URL patterns in the format Django needs.</p>
<p>The first argument to <tt class="docutils literal"><span class="pre">patterns()</span></tt> is a string <tt class="docutils literal"><span class="pre">prefix</span></tt>. See
"The view prefix" below.</p>
<p>The remaining arguments should be tuples in this format:</p>
<div class="highlight-python"><pre>(regular expression, Python callback function [, optional dictionary [, optional name]])</pre>
</div>
<p>...where <tt class="docutils literal"><span class="pre">optional</span> <span class="pre">dictionary</span></tt> and <tt class="docutils literal"><span class="pre">optional</span> <span class="pre">name</span></tt> are optional. (See
<a class="reference internal" href="#passing-extra-options-to-view-functions">Passing extra options to view functions</a> below.)</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Because <cite>patterns()</cite> is a function call, it accepts a maximum of 255
arguments (URL patterns, in this case). This is a limit for all Python
function calls. This is rarely a problem in practice, because you'll
typically structure your URL patterns modularly by using <cite>include()</cite>
sections. However, on the off-chance you do hit the 255-argument limit,
realize that <cite>patterns()</cite> returns a Python list, so you can split up the
construction of the list.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="o">...</span>
    <span class="p">)</span>
<span class="n">urlpatterns</span> <span class="o">+=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="o">...</span>
    <span class="p">)</span>
</pre></div>
</div>
<p class="last">Python lists have unlimited size, so there's no limit to how many URL
patterns you can construct. The only limit is that you can only create 254
at a time (the 255th argument is the initial prefix argument).</p>
</div>
</div>
<div class="section" id="s-url">
<span id="url"></span><h3>url<a class="headerlink" href="#url" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>You can use the <tt class="docutils literal"><span class="pre">url()</span></tt> function, instead of a tuple, as an argument to
<tt class="docutils literal"><span class="pre">patterns()</span></tt>. This is convenient if you want to specify a name without the
optional extra arguments dictionary. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r'^index/$'</span><span class="p">,</span> <span class="n">index_view</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">"main-view"</span><span class="p">),</span>
    <span class="o">...</span>
<span class="p">)</span>
</pre></div>
</div>
<p>This function takes five arguments, most of which are optional:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">url</span><span class="p">(</span><span class="n">regex</span><span class="p">,</span> <span class="n">view</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s">''</span><span class="p">)</span>
</pre></div>
</div>
<p>See <a class="reference internal" href="#id2">Naming URL patterns</a> for why the <tt class="docutils literal"><span class="pre">name</span></tt> parameter is useful.</p>
<p>The <tt class="docutils literal"><span class="pre">prefix</span></tt> parameter has the same meaning as the first argument to
<tt class="docutils literal"><span class="pre">patterns()</span></tt> and is only relevant when you're passing a string as the
<tt class="docutils literal"><span class="pre">view</span></tt> parameter.</p>
</div>
<div class="section" id="s-handler404">
<span id="handler404"></span><h3>handler404<a class="headerlink" href="#handler404" title="Permalink to this headline">¶</a></h3>
<p>A string representing the full Python import path to the view that should be
called if none of the URL patterns match.</p>
<p>By default, this is <tt class="docutils literal"><span class="pre">'django.views.defaults.page_not_found'</span></tt>. That default
value should suffice.</p>
</div>
<div class="section" id="s-handler500">
<span id="handler500"></span><h3>handler500<a class="headerlink" href="#handler500" title="Permalink to this headline">¶</a></h3>
<p>A string representing the full Python import path to the view that should be
called in case of server errors. Server errors happen when you have runtime
errors in view code.</p>
<p>By default, this is <tt class="docutils literal"><span class="pre">'django.views.defaults.server_error'</span></tt>. That default
value should suffice.</p>
</div>
<div class="section" id="s-include">
<span id="include"></span><h3>include<a class="headerlink" href="#include" title="Permalink to this headline">¶</a></h3>
<p>A function that takes a full Python import path to another URLconf that should
be "included" in this place. See <a class="reference internal" href="#including-other-urlconfs">Including other URLconfs</a> below.</p>
</div>
</div>
<div class="section" id="s-notes-on-capturing-text-in-urls">
<span id="notes-on-capturing-text-in-urls"></span><h2>Notes on capturing text in URLs<a class="headerlink" href="#notes-on-capturing-text-in-urls" title="Permalink to this headline">¶</a></h2>
<p>Each captured argument is sent to the view as a plain Python string, regardless
of what sort of match the regular expression makes. For example, in this
URLconf line:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="s">r'^articles/(?P&lt;year&gt;\d{4})/$'</span><span class="p">,</span> <span class="s">'news.views.year_archive'</span><span class="p">),</span>
</pre></div>
</div>
<p>...the <tt class="docutils literal"><span class="pre">year</span></tt> argument to <tt class="docutils literal"><span class="pre">news.views.year_archive()</span></tt> will be a string, not
an integer, even though the <tt class="docutils literal"><span class="pre">\d{4}</span></tt> will only match integer strings.</p>
<p>A convenient trick is to specify default parameters for your views' arguments.
Here's an example URLconf and view:</p>
<div class="highlight-python"><pre># URLconf
urlpatterns = patterns('',
    (r'^blog/$', 'blog.views.page'),
    (r'^blog/page(?P&lt;num&gt;\d+)/$', 'blog.views.page'),
)

# View (in blog/views.py)
def page(request, num="1"):
    # Output the appropriate page of blog entries, according to num.</pre>
</div>
<p>In the above example, both URL patterns point to the same view --
<tt class="docutils literal"><span class="pre">blog.views.page</span></tt> -- but the first pattern doesn't capture anything from the
URL. If the first pattern matches, the <tt class="docutils literal"><span class="pre">page()</span></tt> function will use its
default argument for <tt class="docutils literal"><span class="pre">num</span></tt>, <tt class="docutils literal"><span class="pre">"1"</span></tt>. If the second pattern matches,
<tt class="docutils literal"><span class="pre">page()</span></tt> will use whatever <tt class="docutils literal"><span class="pre">num</span></tt> value was captured by the regex.</p>
</div>
<div class="section" id="s-performance">
<span id="performance"></span><h2>Performance<a class="headerlink" href="#performance" title="Permalink to this headline">¶</a></h2>
<p>Each regular expression in a <tt class="docutils literal"><span class="pre">urlpatterns</span></tt> is compiled the first time it's
accessed. This makes the system blazingly fast.</p>
</div>
<div class="section" id="s-the-view-prefix">
<span id="the-view-prefix"></span><h2>The view prefix<a class="headerlink" href="#the-view-prefix" title="Permalink to this headline">¶</a></h2>
<p>You can specify a common prefix in your <tt class="docutils literal"><span class="pre">patterns()</span></tt> call, to cut down on
code duplication.</p>
<p>Here's the example URLconf from the <a class="reference external" href="http://docs.djangoproject.com/en/1.0/intro/overview/#intro-overview"><em>Django overview</em></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.conf.urls.defaults</span> <span class="k">import</span> <span class="o">*</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^articles/(\d{4})/$'</span><span class="p">,</span> <span class="s">'mysite.news.views.year_archive'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^articles/(\d{4})/(\d{2})/$'</span><span class="p">,</span> <span class="s">'mysite.news.views.month_archive'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^articles/(\d{4})/(\d{2})/(\d+)/$'</span><span class="p">,</span> <span class="s">'mysite.news.views.article_detail'</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>In this example, each view has a common prefix -- <tt class="docutils literal"><span class="pre">'mysite.news.views'</span></tt>.
Instead of typing that out for each entry in <tt class="docutils literal"><span class="pre">urlpatterns</span></tt>, you can use the
first argument to the <tt class="docutils literal"><span class="pre">patterns()</span></tt> function to specify a prefix to apply to
each view function.</p>
<p>With this in mind, the above example can be written more concisely as:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.conf.urls.defaults</span> <span class="k">import</span> <span class="o">*</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">'mysite.news.views'</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^articles/(\d{4})/$'</span><span class="p">,</span> <span class="s">'year_archive'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^articles/(\d{4})/(\d{2})/$'</span><span class="p">,</span> <span class="s">'month_archive'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^articles/(\d{4})/(\d{2})/(\d+)/$'</span><span class="p">,</span> <span class="s">'article_detail'</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Note that you don't put a trailing dot (<tt class="docutils literal"><span class="pre">"."</span></tt>) in the prefix. Django puts
that in automatically.</p>
<div class="section" id="s-multiple-view-prefixes">
<span id="multiple-view-prefixes"></span><h3>Multiple view prefixes<a class="headerlink" href="#multiple-view-prefixes" title="Permalink to this headline">¶</a></h3>
<p>In practice, you'll probably end up mixing and matching views to the point
where the views in your <tt class="docutils literal"><span class="pre">urlpatterns</span></tt> won't have a common prefix. However,
you can still take advantage of the view prefix shortcut to remove duplication.
Just add multiple <tt class="docutils literal"><span class="pre">patterns()</span></tt> objects together, like this:</p>
<p>Old:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.conf.urls.defaults</span> <span class="k">import</span> <span class="o">*</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^$'</span><span class="p">,</span> <span class="s">'django.views.generic.date_based.archive_index'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^(?P&lt;year&gt;\d{4})/(?P&lt;month&gt;[a-z]{3})/$'</span><span class="p">,</span> <span class="s">'django.views.generic.date_based.archive_month'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^tag/(?P&lt;tag&gt;\w+)/$'</span><span class="p">,</span> <span class="s">'weblog.views.tag'</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>New:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.conf.urls.defaults</span> <span class="k">import</span> <span class="o">*</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">'django.views.generic.date_based'</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^$'</span><span class="p">,</span> <span class="s">'archive_index'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^(?P&lt;year&gt;\d{4})/(?P&lt;month&gt;[a-z]{3})/$'</span><span class="p">,</span><span class="s">'archive_month'</span><span class="p">),</span>
<span class="p">)</span>

<span class="n">urlpatterns</span> <span class="o">+=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">'weblog.views'</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^tag/(?P&lt;tag&gt;\w+)/$'</span><span class="p">,</span> <span class="s">'tag'</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-including-other-urlconfs">
<span id="including-other-urlconfs"></span><h2>Including other URLconfs<a class="headerlink" href="#including-other-urlconfs" title="Permalink to this headline">¶</a></h2>
<p>At any point, your <tt class="docutils literal"><span class="pre">urlpatterns</span></tt> can "include" other URLconf modules. This
essentially "roots" a set of URLs below other ones.</p>
<p>For example, here's the URLconf for the <a class="reference external" href="http://www.djangoproject.com/">Django Web site</a> itself. It includes a
number of other URLconfs:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.conf.urls.defaults</span> <span class="k">import</span> <span class="o">*</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^weblog/'</span><span class="p">,</span>        <span class="n">include</span><span class="p">(</span><span class="s">'django_website.apps.blog.urls.blog'</span><span class="p">)),</span>
    <span class="p">(</span><span class="s">r'^documentation/'</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">'django_website.apps.docs.urls.docs'</span><span class="p">)),</span>
    <span class="p">(</span><span class="s">r'^comments/'</span><span class="p">,</span>      <span class="n">include</span><span class="p">(</span><span class="s">'django.contrib.comments.urls'</span><span class="p">)),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Note that the regular expressions in this example don't have a <tt class="docutils literal"><span class="pre">$</span></tt>
(end-of-string match character) but do include a trailing slash. Whenever
Django encounters <tt class="docutils literal"><span class="pre">include()</span></tt>, it chops off whatever part of the URL matched
up to that point and sends the remaining string to the included URLconf for
further processing.</p>
<div class="section" id="s-captured-parameters">
<span id="captured-parameters"></span><h3>Captured parameters<a class="headerlink" href="#captured-parameters" title="Permalink to this headline">¶</a></h3>
<p>An included URLconf receives any captured parameters from parent URLconfs, so
the following example is valid:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># In settings/urls/main.py</span>
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^(?P&lt;username&gt;\w+)/blog/'</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">'foo.urls.blog'</span><span class="p">)),</span>
<span class="p">)</span>

<span class="c"># In foo/urls/blog.py</span>
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">'foo.views'</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^$'</span><span class="p">,</span> <span class="s">'blog.index'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^archive/$'</span><span class="p">,</span> <span class="s">'blog.archive'</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>In the above example, the captured <tt class="docutils literal"><span class="pre">"username"</span></tt> variable is passed to the
included URLconf, as expected.</p>
</div>
</div>
<div class="section" id="s-passing-extra-options-to-view-functions">
<span id="passing-extra-options-to-view-functions"></span><h2>Passing extra options to view functions<a class="headerlink" href="#passing-extra-options-to-view-functions" title="Permalink to this headline">¶</a></h2>
<p>URLconfs have a hook that lets you pass extra arguments to your view functions,
as a Python dictionary.</p>
<p>Any URLconf tuple can have an optional third element, which should be a
dictionary of extra keyword arguments to pass to the view function.</p>
<p>For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">'blog.views'</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^blog/(?P&lt;year&gt;\d{4})/$'</span><span class="p">,</span> <span class="s">'year_archive'</span><span class="p">,</span> <span class="p">{</span><span class="s">'foo'</span><span class="p">:</span> <span class="s">'bar'</span><span class="p">}),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>In this example, for a request to <tt class="docutils literal"><span class="pre">/blog/2005/</span></tt>, Django will call the
<tt class="docutils literal"><span class="pre">blog.views.year_archive()</span></tt> view, passing it these keyword arguments:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">year</span><span class="o">=</span><span class="s">'2005'</span><span class="p">,</span> <span class="n">foo</span><span class="o">=</span><span class="s">'bar'</span>
</pre></div>
</div>
<p>This technique is used in <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/generic-views/#ref-generic-views"><em>generic views</em></a> and in the
<a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/contrib/syndication/#ref-contrib-syndication"><em>syndication framework</em></a> to pass metadata and
options to views.</p>
<div class="admonition-dealing-with-conflicts admonition">
<p class="first admonition-title">Dealing with conflicts</p>
<p class="last">It's possible to have a URL pattern which captures named keyword arguments,
and also passes arguments with the same names in its dictionary of extra
arguments. When this happens, the arguments in the dictionary will be used
instead of the arguments captured in the URL.</p>
</div>
<div class="section" id="s-passing-extra-options-to-include">
<span id="passing-extra-options-to-include"></span><h3>Passing extra options to <tt class="docutils literal"><span class="pre">include()</span></tt><a class="headerlink" href="#passing-extra-options-to-include" title="Permalink to this headline">¶</a></h3>
<p>Similarly, you can pass extra options to <tt class="docutils literal"><span class="pre">include()</span></tt>. When you pass extra
options to <tt class="docutils literal"><span class="pre">include()</span></tt>, <em>each</em> line in the included URLconf will be passed
the extra options.</p>
<p>For example, these two URLconf sets are functionally identical:</p>
<p>Set one:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># main.py</span>
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^blog/'</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">'inner'</span><span class="p">),</span> <span class="p">{</span><span class="s">'blogid'</span><span class="p">:</span> <span class="mf">3</span><span class="p">}),</span>
<span class="p">)</span>

<span class="c"># inner.py</span>
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^archive/$'</span><span class="p">,</span> <span class="s">'mysite.views.archive'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^about/$'</span><span class="p">,</span> <span class="s">'mysite.views.about'</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Set two:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># main.py</span>
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^blog/'</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s">'inner'</span><span class="p">)),</span>
<span class="p">)</span>

<span class="c"># inner.py</span>
<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^archive/$'</span><span class="p">,</span> <span class="s">'mysite.views.archive'</span><span class="p">,</span> <span class="p">{</span><span class="s">'blogid'</span><span class="p">:</span> <span class="mf">3</span><span class="p">}),</span>
    <span class="p">(</span><span class="s">r'^about/$'</span><span class="p">,</span> <span class="s">'mysite.views.about'</span><span class="p">,</span> <span class="p">{</span><span class="s">'blogid'</span><span class="p">:</span> <span class="mf">3</span><span class="p">}),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Note that extra options will <em>always</em> be passed to <em>every</em> line in the included
URLconf, regardless of whether the line's view actually accepts those options
as valid. For this reason, this technique is only useful if you're certain that
every view in the included URLconf accepts the extra options you're passing.</p>
</div>
</div>
<div class="section" id="s-passing-callable-objects-instead-of-strings">
<span id="passing-callable-objects-instead-of-strings"></span><h2>Passing callable objects instead of strings<a class="headerlink" href="#passing-callable-objects-instead-of-strings" title="Permalink to this headline">¶</a></h2>
<p>Some developers find it more natural to pass the actual Python function object
rather than a string containing the path to its module. This alternative is
supported -- you can pass any callable object as the view.</p>
<p>For example, given this URLconf in "string" notation:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^archive/$'</span><span class="p">,</span> <span class="s">'mysite.views.archive'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^about/$'</span><span class="p">,</span> <span class="s">'mysite.views.about'</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^contact/$'</span><span class="p">,</span> <span class="s">'mysite.views.contact'</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>You can accomplish the same thing by passing objects rather than strings. Just
be sure to import the objects:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">mysite.views</span> <span class="k">import</span> <span class="n">archive</span><span class="p">,</span> <span class="n">about</span><span class="p">,</span> <span class="n">contact</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^archive/$'</span><span class="p">,</span> <span class="n">archive</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^about/$'</span><span class="p">,</span> <span class="n">about</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^contact/$'</span><span class="p">,</span> <span class="n">contact</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The following example is functionally identical. It's just a bit more compact
because it imports the module that contains the views, rather than importing
each view individually:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">mysite</span> <span class="k">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^archive/$'</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">archive</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^about/$'</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">about</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^contact/$'</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">contact</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The style you use is up to you.</p>
<p>Note that if you use this technique -- passing objects rather than strings --
the view prefix (as explained in "The view prefix" above) will have no effect.</p>
</div>
<div class="section" id="s-id2">
<span id="s-naming-url-patterns"></span><span id="id2"></span><span id="naming-url-patterns"></span><h2>Naming URL patterns<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference external" href="http://docs.djangoproject.com/en/1.0/releases/1.0/#releases-1-0"><em>Please, see the release notes</em></a></div>
<p>It's fairly common to use the same view function in multiple URL patterns in
your URLconf. For example, these two URL patterns both point to the <tt class="docutils literal"><span class="pre">archive</span></tt>
view:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="p">(</span><span class="s">r'^archive/(\d{4})/$'</span><span class="p">,</span> <span class="n">archive</span><span class="p">),</span>
    <span class="p">(</span><span class="s">r'^archive-summary/(\d{4})/$'</span><span class="p">,</span> <span class="n">archive</span><span class="p">,</span> <span class="p">{</span><span class="s">'summary'</span><span class="p">:</span> <span class="bp">True</span><span class="p">}),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>This is completely valid, but it leads to problems when you try to do reverse
URL matching (through the <tt class="docutils literal"><span class="pre">permalink()</span></tt> decorator or the <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/templates/builtins/#ttag-url"><tt class="xref docutils literal"><span class="pre">url</span></tt></a> template
tag. Continuing this example, if you wanted to retrieve the URL for the
<tt class="docutils literal"><span class="pre">archive</span></tt> view, Django's reverse URL matcher would get confused, because <em>two</em>
URLpatterns point at that view.</p>
<p>To solve this problem, Django supports <strong>named URL patterns</strong>. That is, you can
give a name to a URL pattern in order to distinguish it from other patterns
using the same view and parameters. Then, you can use this name in reverse URL
matching.</p>
<p>Here's the above example, rewritten to use named URL patterns:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">''</span><span class="p">,</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r'^archive/(\d{4})/$'</span><span class="p">,</span> <span class="n">archive</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">"full-archive"</span><span class="p">),</span>
    <span class="n">url</span><span class="p">(</span><span class="s">r'^archive-summary/(\d{4})/$'</span><span class="p">,</span> <span class="n">archive</span><span class="p">,</span> <span class="p">{</span><span class="s">'summary'</span><span class="p">:</span> <span class="bp">True</span><span class="p">},</span> <span class="s">"arch-summary"</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>With these names in place (<tt class="docutils literal"><span class="pre">full-archive</span></tt> and <tt class="docutils literal"><span class="pre">arch-summary</span></tt>), you can
target each pattern individually by using its name:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">url</span> <span class="nv">arch</span><span class="o">-</span><span class="nv">summary</span> <span class="m">1945</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">url</span> <span class="nv">full</span><span class="o">-</span><span class="nv">archive</span> <span class="m">2007</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Even though both URL patterns refer to the <tt class="docutils literal"><span class="pre">archive</span></tt> view here, using the
<tt class="docutils literal"><span class="pre">name</span></tt> parameter to <tt class="docutils literal"><span class="pre">url()</span></tt> allows you to tell them apart in templates.</p>
<p>The string used for the URL name can contain any characters you like. You are
not restricted to valid Python names.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>When you name your URL patterns, make sure you use names that are unlikely
to clash with any other application's choice of names. If you call your URL
pattern <tt class="docutils literal"><span class="pre">comment</span></tt>, and another application does the same thing, there's
no guarantee which URL will be inserted into your template when you use
this name.</p>
<p class="last">Putting a prefix on your URL names, perhaps derived from the application
name, will decrease the chances of collision. We recommend something like
<tt class="docutils literal"><span class="pre">myapp-comment</span></tt> instead of <tt class="docutils literal"><span class="pre">comment</span></tt>.</p>
</div>
</div>
<div class="section" id="s-utility-methods">
<span id="utility-methods"></span><h2>Utility methods<a class="headerlink" href="#utility-methods" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-reverse">
<span id="reverse"></span><h3>reverse()<a class="headerlink" href="#reverse" title="Permalink to this headline">¶</a></h3>
<p>If you need to use something similar to the <a class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/templates/builtins/#ttag-url"><tt class="xref docutils literal"><span class="pre">url</span></tt></a> template tag in
your code, Django provides the following method (in the
<tt class="docutils literal"><span class="pre">django.core.urlresolvers</span></tt> module):</p>
<dl class="function">
<dt id="django.core.urlresolvers.reverse">
<!--[django.core.urlresolvers.reverse]--><tt class="descname">reverse</tt>(<em>viewname</em>, <em>urlconf=None</em>, <em>args=None</em>, <em>kwargs=None</em>)<a class="headerlink" href="#django.core.urlresolvers.reverse" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p><tt class="docutils literal"><span class="pre">viewname</span></tt> is either the function name (either a function reference, or the
string version of the name, if you used that form in <tt class="docutils literal"><span class="pre">urlpatterns</span></tt>) or the
<a class="reference internal" href="#id2">URL pattern name</a>.  Normally, you won't need to worry about the
<tt class="docutils literal"><span class="pre">urlconf</span></tt> parameter and will only pass in the positional and keyword
arguments to use in the URL matching. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">django.core.urlresolvers</span> <span class="k">import</span> <span class="n">reverse</span>

<span class="k">def</span> <span class="nf">myview</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">reverse</span><span class="p">(</span><span class="s">'arch-summary'</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">[</span><span class="mf">1945</span><span class="p">]))</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">reverse()</span></tt> function can reverse a large variety of regular expression
patterns for URLs, but not every possible one. The main restriction at the
moment is that the pattern cannot contain alternative choices using the
vertical bar (<tt class="docutils literal"><span class="pre">"|"</span></tt>) character. You can quite happily use such patterns for
matching against incoming URLs and sending them off to views, but you cannot
reverse such patterns.</p>
<div class="admonition-make-sure-your-views-are-all-correct admonition">
<p class="first admonition-title">Make sure your views are all correct</p>
<p>As part of working out which URL names map to which patterns, the
<tt class="docutils literal"><span class="pre">reverse()</span></tt> function has to import all of your URLconf files and examine
the name of each view. This involves importing each view function. If
there are <em>any</em> errors whilst importing any of your view functions, it
will cause <tt class="docutils literal"><span class="pre">reverse()</span></tt> to raise an error, even if that view function is
not the one you are trying to reverse.</p>
<p class="last">Make sure that any views you reference in your URLconf files exist and can
be imported correctly. Do not include lines that reference views you
haven't written yet, because those views will not be importable.</p>
</div>
</div>
<div class="section" id="s-resolve">
<span id="resolve"></span><h3>resolve()<a class="headerlink" href="#resolve" title="Permalink to this headline">¶</a></h3>
<p>The <a title="django.core.urlresolvers.resolve" class="reference internal" href="#django.core.urlresolvers.resolve"><tt class="xref docutils literal"><span class="pre">django.core.urlresolvers.resolve()</span></tt></a> function can be used for resolving
URL paths to the corresponding view functions. It has the following signature:</p>
<dl class="function">
<dt id="django.core.urlresolvers.resolve">
<!--[django.core.urlresolvers.resolve]--><tt class="descname">resolve</tt>(<em>path</em>, <em>urlconf=None</em>)<a class="headerlink" href="#django.core.urlresolvers.resolve" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p><tt class="docutils literal"><span class="pre">path</span></tt> is the URL path you want to resolve. As with <tt class="docutils literal"><span class="pre">reverse()</span></tt> above, you
don't need to worry about the <tt class="docutils literal"><span class="pre">urlconf</span></tt> parameter. The function returns the
triple (view function, arguments, keyword arguments).</p>
<p>For example, it can be used for testing if a view would raise a <tt class="docutils literal"><span class="pre">Http404</span></tt>
error before redirecting to it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">from</span> <span class="nn">urlparse</span> <span class="k">import</span> <span class="n">urlparse</span>
<span class="k">from</span> <span class="nn">django.core.urlresolvers</span> <span class="k">import</span> <span class="n">resolve</span>
<span class="k">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponseRedirect</span><span class="p">,</span> <span class="n">Http404</span>

<span class="k">def</span> <span class="nf">myview</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="n">next</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'HTTP_REFERER'</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="ow">or</span> <span class="s">'/'</span>
    <span class="n">response</span> <span class="o">=</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">next</span><span class="p">)</span>

    <span class="c"># modify the request and response as required, e.g. change locale</span>
    <span class="c"># and set corresponding locale cookie</span>

    <span class="n">view</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span> <span class="o">=</span> <span class="n">resolve</span><span class="p">(</span><span class="n">urlparse</span><span class="p">(</span><span class="n">next</span><span class="p">)[</span><span class="mf">2</span><span class="p">])</span>
    <span class="n">kwargs</span><span class="p">[</span><span class="s">'request'</span><span class="p">]</span> <span class="o">=</span> <span class="n">request</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">view</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">Http404</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">response</span>
</pre></div>
</div>
</div>
<div class="section" id="s-permalink">
<span id="permalink"></span><h3>permalink()<a class="headerlink" href="#permalink" title="Permalink to this headline">¶</a></h3>
<p>The <a title="django.db.models.permalink" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/instances/#django.db.models.permalink"><tt class="xref docutils literal"><span class="pre">django.db.models.permalink()</span></tt></a> decorator is useful for writing short
methods that return a full URL path. For example, a model's
<tt class="docutils literal"><span class="pre">get_absolute_url()</span></tt> method. See <a title="django.db.models.permalink" class="reference external" href="http://docs.djangoproject.com/en/1.0/ref/models/instances/#django.db.models.permalink"><tt class="xref docutils literal"><span class="pre">django.db.models.permalink()</span></tt></a> for more.</p>
</div>
</div>
</div>



<div id="content-secondary">
  <h2 id="comments">Questions/Feedback</h2>
  <p>Having trouble? We'd like to help!</p>
  <ul>
    <li>
      Try the <a href="http://docs.djangoproject.com/en/dev/faq/">FAQ</a> — it's got answers to many common
      questions.
    </li>
    <li>
      Search for information in the <a href="http://groups.google.com/group/django-users/">archives of the
      django-users mailing list</a>, or <a href="http://groups.google.com/group/django-users/">post a question</a>.
    </li>
    <li>
      Ask a question in the <a href="irc://irc.freenode.net/">#django IRC
      channel</a>, or search the <a href="http://oebfare.com/logger/django/">IRC
      logs</a> to see if its been asked before.
    </li>
    <li>
      If you notice errors with this documentation, please <a href="http://code.djangoproject.com/simpleticket?component=Documentation">
      open a ticket</a> and let us know! Please only use the ticket tracker for
      criticisms and improvements on the docs. For tech support, use the
      resources above.
    </li>
  </ul>
</div>

		</div>
		<!-- END #content-main -->
		<div id="content-related" class="sidebar">
		
  
    <h2>Contents</h2>
    
      <ul>
<li><a class="reference external" href="">URL dispatcher</a><ul>
<li><a class="reference external" href="#overview">Overview</a></li>
<li><a class="reference external" href="#id1">How Django processes a request</a></li>
<li><a class="reference external" href="#example">Example</a></li>
<li><a class="reference external" href="#named-groups">Named groups</a><ul>
<li><a class="reference external" href="#the-matching-grouping-algorithm">The matching/grouping algorithm</a></li>
</ul>
</li>
<li><a class="reference external" href="#what-the-urlconf-searches-against">What the URLconf searches against</a></li>
<li><a class="reference external" href="#syntax-of-the-urlpatterns-variable">Syntax of the urlpatterns variable</a><ul>
<li><a class="reference external" href="#patterns">patterns</a></li>
<li><a class="reference external" href="#url">url</a></li>
<li><a class="reference external" href="#handler404">handler404</a></li>
<li><a class="reference external" href="#handler500">handler500</a></li>
<li><a class="reference external" href="#include">include</a></li>
</ul>
</li>
<li><a class="reference external" href="#notes-on-capturing-text-in-urls">Notes on capturing text in URLs</a></li>
<li><a class="reference external" href="#performance">Performance</a></li>
<li><a class="reference external" href="#the-view-prefix">The view prefix</a><ul>
<li><a class="reference external" href="#multiple-view-prefixes">Multiple view prefixes</a></li>
</ul>
</li>
<li><a class="reference external" href="#including-other-urlconfs">Including other URLconfs</a><ul>
<li><a class="reference external" href="#captured-parameters">Captured parameters</a></li>
</ul>
</li>
<li><a class="reference external" href="#passing-extra-options-to-view-functions">Passing extra options to view functions</a><ul>
<li><a class="reference external" href="#passing-extra-options-to-include">Passing extra options to <tt class="docutils literal"><span class="pre">include()</span></tt></a></li>
</ul>
</li>
<li><a class="reference external" href="#passing-callable-objects-instead-of-strings">Passing callable objects instead of strings</a></li>
<li><a class="reference external" href="#id2">Naming URL patterns</a></li>
<li><a class="reference external" href="#utility-methods">Utility methods</a><ul>
<li><a class="reference external" href="#reverse">reverse()</a></li>
<li><a class="reference external" href="#resolve">resolve()</a></li>
<li><a class="reference external" href="#permalink">permalink()</a></li>
</ul>
</li>
</ul>
</li>
</ul>

    
  
  
  
    <h2>Search</h2>
    
    <form action="/en/1.0/search/" id="search" class="search">
  <div>
    <input name="cx" value="009763561546736975936:e88ek0eurf4" type="hidden">
    <input name="cof" value="FORID:11" type="hidden">
    <input name="ie" value="UTF-8" type="hidden">
    <input name="hl" value="" type="hidden">
    <input style="background: rgb(255, 255, 255) url(http://www.google.com/coop/intl/en/images/google_custom_search_watermark.gif) no-repeat scroll left center; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" id="id_search_q" class="query" name="q" type="text">
    <input name="sa" class="submit" value="Search" type="submit">
    <ul>
<li><label for="id_search_as_q_0"><input checked="checked" id="id_search_as_q_0" value="more:dev_docs" name="as_q" type="radio"> Latest</label></li>
<li><label for="id_search_as_q_1"><input id="id_search_as_q_1" value="more:1.0_docs" name="as_q" type="radio"> 1.0</label></li>
<li><label for="id_search_as_q_2"><input id="id_search_as_q_2" value="more:0.96_docs" name="as_q" type="radio"> 0.96</label></li>
<li><label for="id_search_as_q_3"><input id="id_search_as_q_3" value="more:all_docs" name="as_q" type="radio"> All</label></li>
</ul>
  </div>
</form>
<script type="text/javascript" src="Django%20%7C%20URL%20dispatcher%20%7C%20Django%20Documentation_files/brand.html"></script>
  
  
  
    <h2>Browse</h2>
    <ul>
      
        
          <li>Prev: <a href="http://docs.djangoproject.com/en/1.0/topics/http/">Handling HTTP requests</a></li>
        
        
          <li>Next: <a href="http://docs.djangoproject.com/en/1.0/topics/http/views/">Writing Views</a></li>
        
        <li><a href="http://docs.djangoproject.com/en/1.0/contents/">Table of contents</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/1.0/genindex/">General Index</a></li>
        
          <li><a href="http://docs.djangoproject.com/en/1.0/modindex/">Global Module Index</a></li>
        
      
    </ul>
  
  
  
    <h2>You are here:</h2>
    <ul>
      
        <li>
          <a href="http://docs.djangoproject.com/en/1.0/">Django 1.0 documentation</a>
          
            <ul><li><a href="http://docs.djangoproject.com/en/1.0/topics/">Using Django</a>
          
            <ul><li><a href="http://docs.djangoproject.com/en/1.0/topics/http/">Handling HTTP requests</a>
          
          <ul><li>URL dispatcher</li></ul>
          </li></ul></li></ul>
        </li>
      
    </ul>
  
  
  
    <h3>Last update:</h3>
    <div>May 23, 2009, 8:15 a.m. (<a href="http://www.timeanddate.com/worldclock/city.html?n=64">CDT</a>)</div>
  

		</div>
		<!-- END #content-related -->

		</div>
		<!-- END #content -->
		<div id="footer">
			<p>© 2005-2009 <a href="http://www.djangoproject.com/foundation/">Django Software Foundation</a> unless otherwise noted. Django is a registered trademark of the Django Software Foundation. 
			Hosting graciously provided by <a href="http://mediatemple.net/">
			<img style="vertical-align: middle; position: relative; top: -1px;" src="Django%20%7C%20URL%20dispatcher%20%7C%20Django%20Documentation_files/mt.png" alt="media temple"></a>
			</p>
		</div>
		<!-- END #footer -->
	</div>
	<!-- END #container -->
	</body></html>